package com.sanjiahantang.youjiaclient.module.programmeprice.views.impl;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.module.invoice.views.impl.InvoiceActivity;
import com.sanjiahantang.youjiaclient.module.me.address.beans.AddressBean;
import com.sanjiahantang.youjiaclient.module.me.address.views.AddressView;
import com.sanjiahantang.youjiaclient.module.me.address.views.impl.AddressListActivity;
import com.sanjiahantang.youjiaclient.module.me.order.views.OrderView;
import com.sanjiahantang.youjiaclient.module.programmeprice.beans.ProgramBean;
import com.sanjiahantang.youjiaclient.module.programmeprice.beans.ProgramContractBean;
import com.sanjiahantang.youjiaclient.module.programmeprice.presenters.ProgramPricePresenter;
import com.sanjiahantang.youjiaclient.module.programmeprice.views.ProgramPriceView;

/**
 * Created by admin on 2017/10/25.
 */

public class ProgramChoiceInfoActivity extends BaseMvpActivity<ProgramPriceView, ProgramPricePresenter> implements ProgramPriceView, View.OnClickListener {

    private TextView title, txtRight, txtcomit, txtTotal, infoName, infoPhone, infoDetail_address;
    private EditText inMessage;
    private View flLeft, flIcon3, yesAddress, noAddress,panelContract,panelMessage,panelContractSigned;
    private RecyclerView recyclerView;
    private CheckBox Contract;
    private BaseView.OnLoadListener listener;
    private String Title_Content, Title_No, isPersonCompany;
    private int addID;
    private ProgramContractBean.Data contractData;
    private float totalPrice = 0f;
    private int type;
    private String ppId,version;
    private ProgramBean.Data programData;
    private int isOfficial = 1;

    @Override
    public ProgramPricePresenter initPresenter() {
        return new ProgramPricePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_program_choiceinfo);
        title = (TextView) findViewById(R.id.title);
        type = getIntent().getIntExtra(KEY_TYPE,0);
        findViewById(R.id.fl_Left).setOnClickListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        ppId = getIntent().getStringExtra(KEY_ID);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setFocusableInTouchMode(false);
        recyclerView.setNestedScrollingEnabled(false);
        txtcomit = (TextView) findViewById(R.id.txtcomit);
        //地址
        infoDetail_address = (TextView) findViewById(R.id.infoDetail_address);
        infoPhone = (TextView) findViewById(R.id.infoPhone);
        infoName = (TextView) findViewById(R.id.infoName);
        //显示收货地址
        yesAddress = findViewById(R.id.yesAddress);
        yesAddress.setOnClickListener(this);
        //没有收货地址
        noAddress = findViewById(R.id.noAddress);
        noAddress.setOnClickListener(this);
        txtTotal = (TextView) findViewById(R.id.txtTotal);

        //留言信息
        inMessage = (EditText) findViewById(R.id.inMessage);
        //合同选择
        Contract = (CheckBox) findViewById(R.id.Contract);
        panelContract = findViewById(R.id.panelContract);
        panelContractSigned = findViewById(R.id.panelContractSigned);
        panelMessage = findViewById(R.id.panelMessage);
        panelContract.setOnClickListener(this);
        txtcomit.setOnClickListener(this);
        title.setText("确认订单");
        if (Integer.valueOf(ppId) == 0){
            Utils.showToast($this,"订单找不到了");
            finish();
            return;
        }
        presenter.setOnLoadListener(new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] param = (Object[]) content;
                switch ((int) param[0]) {
                    case CALLBACK_TYPE_RECYCLER_ADAPTER:
                        recyclerView.setAdapter((RecyclerView.Adapter) param[1]);
                        break;
                    case CALLBACK_TYPE_QUICK_ADAPTER:
                        float price = ((Float)param[1]);
                        totalPrice = price;
                        txtTotal.setText(String.format("%.2f",totalPrice));
                        break;
                    case CALLBACK_TYPE_ADDRESS:
                        AddressBean.Data data = (AddressBean.Data) param[1];
                        addID = data.getUcaId();
                        infoName.setText(data.getCustomerName());
                        infoPhone.setText(data.getCellPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                        infoDetail_address.setText(data.getAddress());
                        yesAddress.setVisibility(View.VISIBLE);
                        noAddress.setVisibility(View.GONE);
                        break;
                    case CALLBACK_TYPE_ORDER_DATA:
                        programData = (ProgramBean.Data) param[1];
                        if (programData != null){
                            if (type != TYPE_INCREASE){
                                if (programData.getServiceList() == null || programData.getServiceList().size() == 0){
                                    panelContract.setVisibility(View.GONE);
                                    panelMessage.setVisibility(View.GONE);
                                    isOfficial = 0;
                                }else{
                                    panelContract.setVisibility(View.VISIBLE);
                                    panelMessage.setVisibility(View.VISIBLE);
                                    isOfficial = 1;
                                }
                            }else{
                                panelContract.setVisibility(View.VISIBLE);
                                panelMessage.setVisibility(View.VISIBLE);
                                isOfficial = 1;
                            }
                        }
                        break;
                }
            }
        });

    }

    @Override
    public void initData() {
        presenter.findPlaceOrder(this,Integer.valueOf(ppId),type);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.txtcomit://提交订单

                if (programData != null){

                    if (type != TYPE_INCREASE && (programData.getServiceList() == null || programData.getServiceList().size() == 0)){
                        presenter.creatOrderPhoneForPlan(this,
                                addID,Integer.valueOf(ppId),inMessage.getText().toString()
                                ,type
                        );
                    }else{
                        if (Contract.isChecked()){
                            presenter.creatOrderPhoneForPlan(this,
                                    addID,Integer.valueOf(ppId),inMessage.getText().toString()
                                    ,type
                            );
                        }else{
                            Utils.showToast($this,"请签署优家库装修合同");
                        }
                    }

                }else{
                    Utils.showToast($this,"网络开小差了");
                }

                break;
            case R.id.yesAddress:
            case R.id.noAddress:
                startActivityForResult(new Intent($this, AddressListActivity.class)
                        .putExtra(AddressView.KEY_ORDER,true)
                        , OrderView.INTENT_ADDRESS);
                //跳转到选择地址页面
                break;
            case R.id.panelContract:
                if (!Contract.isChecked()){
                    presenter.indexContractInfo($this, new OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            if (content instanceof ProgramContractBean.Data){
                                contractData = (ProgramContractBean.Data) content;
                            }
                        }
                    });
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case OrderView.INTENT_ADDRESS:
                if (data != null) {
                    yesAddress.setVisibility(View.VISIBLE);
                    noAddress.setVisibility(View.GONE);
                    addID = data.getIntExtra(AddressView.KEY_NAME,0);
                    infoName.setText(data.getStringExtra(AddressView.KEY_NAME));
                    infoPhone.setText(data.getStringExtra(AddressView.KEY_PHONE).replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                    infoDetail_address.setText(data.getStringExtra(AddressView.KEY_ADDRESS));
                } else {
                    yesAddress.setVisibility(View.GONE);
                    noAddress.setVisibility(View.VISIBLE);
                }

                break;
            case 2:
                if (resultCode == RESULT_OK) {
                    presenter.saveContractForPhoneProject(
                            $this,contractData,
                            totalPrice,
                            programData,
                            new OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            Contract.setChecked(true);
                            panelContract.setVisibility(View.GONE);
                            panelContractSigned.setVisibility(View.VISIBLE);
                        }
                    });

                }
                break;
        }
    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

}
